package com.hcy.asyncfiledownload.datamodule.service.provider.store;

import com.hcy.asyncfiledownload.common.entry.ExportRecord;
import com.hcy.asyncfiledownload.common.enums.ExportStatus;
import com.hcy.asyncfiledownload.datamodule.annotion.MyExportProvider;
import com.hcy.asyncfiledownload.datamodule.annotion.ProviderType;
import com.hcy.asyncfiledownload.datamodule.fileUtil.MockFileOpt;
import com.hcy.asyncfiledownload.datamodule.service.mybatis.MockMybatisService;
import com.hcy.asyncfiledownload.datamodule.service.provider.AbstractExportProvider;
import com.hcy.asyncfiledownload.mockdatabase.MockData;
import com.hcy.asyncfiledownload.ordermodule.controller.OrderController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author huchenying
 * @Date 2022/9/6 15:29
 * @Description 订单导出最后的执行者
 */
@Component
@MyExportProvider(ProviderType.OrderExport)
public class OrderExportProvider extends AbstractExportProvider {

    @Autowired
    MockMybatisService service;

    @Autowired
    MockFileOpt mockFileOpt;

    @Autowired
    OrderController orderController;

    @Autowired
    MockMybatisService mockMybatisService;

    @Override
    public void handler() {
        // 获取record
        ExportRecord record = getRecord();
        // 修改导出状态
        ExportRecord exportRecord = MockData.getExportRecordById(record.getId());
        if (exportRecord == null) {
            return;
        }
        exportRecord.setStatusCode(ExportStatus.RUNTIME.getCode());
        mockMybatisService.update(exportRecord);
        //获取查询条件，之后可以进行反序列化
        String queryString = record.getQueryString();
        // 调用本地服务获取数据
        service.getData(queryString);
        // 或者远程服务
        // openfeignClient.getData(queryString)
        List<ExportRecord> exportList = orderController.getExportList();

        // 调用文件生成上传的工具
        String url = mockFileOpt.exec(record.getId(), exportList);
        exportRecord.setUrl(url);
        // 失败则返回失败信息
        // exportRecord.setStatusCode(ExportStatus.Fail.getCode());
        // exportRecord.setErrorMsg("error");
        mockMybatisService.update(exportRecord);

        // 删除threadLocal
        removeThreadLocal();
    }
}
